/******************************************************************************
//             INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in accordance with the terms of that agreement.
//        Copyright (c) 2000-2001 Intel Corporation. All Rights Reserved.
//
//  Description:
//      Intel(R) JPEG library for X platform 
//
********************************************************************************/
#include "_ijx.h"

extern const Ipp8u _nIZigZagIndex[64];

/******************************************************************************
// Name:            ijHuffmanDecode_Gray_BLS
// Description:     Huffman decoder for 1 component image.
// Input Arguments:
//         pStream: Identifies IJBITTREAMSTATE data structure.
//    pDecodeState: Identifies IJDECODESTATE strcuture.
// Output Arguments:
//            pMCUBuf: Identifies decoded MCU.
//                          
// Returns:          
//        [Success] IJX_RTCODE_OK
//        [Failure] IJX_RTCODE_BAGARG
//                  IJX_RTCODE_ERROR
//                     
//                  
******************************************************************************/
IJXRESULT ijHuffmanDecode_Gray_BLS(Ipp16s **ppMCUBuf, 
                                   IJBITTREAMSTATE *pStream,
                                   IJDECODESTATE *pDecodeState)
{
    IJXRESULT rtCode;

    rtCode=ippiDecodeHuffman_Direct_JPEG_1u16s (pStream->pBitStream,  
                                         &(pStream->nUsedBufBitsLen),
                                         ppMCUBuf[0],
                                         &(pDecodeState->dcPred[0]),
                                         &(pStream->nMarker),
                                         &(pStream->nPrefetchedBits),
                                         &(pStream->nNumValidPrefetchedBits),
                                         pDecodeState->paIdxDCHuffTable[0],
                                         pDecodeState->paIdxACHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

//    pDecodeState->counter.nScanMCUCounter++;

    if ( (0<pDecodeState->nRstInterval) ){
        pDecodeState->counter.nDRICounter--;
        if (0==pDecodeState->counter.nDRICounter) {
            ijAdjustRestartIntervalInv(pStream, pDecodeState);
        }
    }

    return IJX_RTCODE_OK;
}

/******************************************************************************
// Name:            ijHuffmanDecode_444_BLS
// Description:     Huffman decoder for 444 image.
// Input Arguments:
//         pStream: Identifies IJBITTREAMSTATE data structure.
//    pDecodeState: Identifies IJDECODESTATE strcuture.
// Output Arguments:
//            pMCUBuf: Identifies decoded MCU.
//                          
// Returns:          
//        [Success] IJX_RTCODE_OK
//        [Failure] IJX_RTCODE_BAGARG
//                  IJX_RTCODE_ERROR
//                     
//                  
******************************************************************************/
IJXRESULT ijHuffmanDecode_444_BLS(Ipp16s **ppMCUBuf, 
                           IJBITTREAMSTATE *pStream,
                           IJDECODESTATE *pDecodeState)
{
    IJXRESULT rtCode;
    rtCode=ippiDecodeHuffman_Direct_JPEG_1u16s (pStream->pBitStream,  
                                         &(pStream->nUsedBufBitsLen),
                                         ppMCUBuf[0],
                                         &(pDecodeState->dcPred[0]),
                                         &(pStream->nMarker),
                                         &(pStream->nPrefetchedBits),
                                         &(pStream->nNumValidPrefetchedBits),
                                         pDecodeState->paIdxDCHuffTable[0],
                                         pDecodeState->paIdxACHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    rtCode=ippiDecodeHuffman_Direct_JPEG_1u16s (pStream->pBitStream,  
                                         &(pStream->nUsedBufBitsLen),
                                         ppMCUBuf[1],
                                         &(pDecodeState->dcPred[1]),
                                         &(pStream->nMarker),
                                         &(pStream->nPrefetchedBits),
                                         &(pStream->nNumValidPrefetchedBits),
                                         pDecodeState->paIdxDCHuffTable[1],
                                         pDecodeState->paIdxACHuffTable[1]);
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    rtCode=ippiDecodeHuffman_Direct_JPEG_1u16s (pStream->pBitStream,  
                                         &(pStream->nUsedBufBitsLen),
                                         ppMCUBuf[2],
                                         &(pDecodeState->dcPred[2]),
                                         &(pStream->nMarker),
                                         &(pStream->nPrefetchedBits),
                                         &(pStream->nNumValidPrefetchedBits),
                                         pDecodeState->paIdxDCHuffTable[2],
                                         pDecodeState->paIdxACHuffTable[2]);

    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }

//    pDecodeState->counter.nScanMCUCounter++;

    if ( (0<pDecodeState->nRstInterval) ){
        pDecodeState->counter.nDRICounter--;
        if (0==pDecodeState->counter.nDRICounter) {
            ijAdjustRestartIntervalInv(pStream, pDecodeState);
        }
    }

    return IJX_RTCODE_OK;
}

/******************************************************************************
// Name:            ijHuffmanDecode_422_BLS
// Description:     Huffman decoder for 422 image.
// Input Arguments:
//         pStream: Identifies IJBITTREAMSTATE data structure.
//    pDecodeState: Identifies IJDECODESTATE strcuture.
// Output Arguments:
//            pMCUBuf: Identifies decoded MCU.
//                          
// Returns:          
//        [Success] IJX_RTCODE_OK
//        [Failure] IJX_RTCODE_BAGARG
//                  IJX_RTCODE_ERROR
//                     
//                  
******************************************************************************/
IJXRESULT ijHuffmanDecode_422_BLS(Ipp16s **ppMCUBuf, 
                           IJBITTREAMSTATE *pStream,
                           IJDECODESTATE *pDecodeState)
{
    IJXRESULT rtCode;

    rtCode=ippiDecodeHuffman_Direct_JPEG_1u16s (pStream->pBitStream,  
                                         &(pStream->nUsedBufBitsLen),
                                         ppMCUBuf[0],
                                         &(pDecodeState->dcPred[0]),
                                         &(pStream->nMarker),
                                         &(pStream->nPrefetchedBits),
                                         &(pStream->nNumValidPrefetchedBits),
                                         pDecodeState->paIdxDCHuffTable[0],
                                         pDecodeState->paIdxACHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    rtCode=ippiDecodeHuffman_Direct_JPEG_1u16s (pStream->pBitStream,  
                                         &(pStream->nUsedBufBitsLen),
                                         &(ppMCUBuf[0][64]),
                                         &(pDecodeState->dcPred[0]),
                                         &(pStream->nMarker),
                                         &(pStream->nPrefetchedBits),
                                         &(pStream->nNumValidPrefetchedBits),
                                         pDecodeState->paIdxDCHuffTable[0],
                                         pDecodeState->paIdxACHuffTable[0]);
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }

    rtCode=ippiDecodeHuffman_Direct_JPEG_1u16s (pStream->pBitStream,  
                                         &(pStream->nUsedBufBitsLen),
                                         ppMCUBuf[1],
                                         &(pDecodeState->dcPred[1]),
                                         &(pStream->nMarker),
                                         &(pStream->nPrefetchedBits),
                                         &(pStream->nNumValidPrefetchedBits),
                                         pDecodeState->paIdxDCHuffTable[1],
                                         pDecodeState->paIdxACHuffTable[1]);
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    rtCode=ippiDecodeHuffman_Direct_JPEG_1u16s (pStream->pBitStream,  
                                         &(pStream->nUsedBufBitsLen),
                                         ppMCUBuf[2],
                                         &(pDecodeState->dcPred[2]),
                                         &(pStream->nMarker),
                                         &(pStream->nPrefetchedBits),
                                         &(pStream->nNumValidPrefetchedBits),
                                         pDecodeState->paIdxDCHuffTable[2],
                                         pDecodeState->paIdxACHuffTable[2]);
    
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

//    pDecodeState->counter.nScanMCUCounter++;

    if ( (0<pDecodeState->nRstInterval) ){
        pDecodeState->counter.nDRICounter--;
        if (0==pDecodeState->counter.nDRICounter) {
            ijAdjustRestartIntervalInv(pStream, pDecodeState);
        }
    }
    
    return IJX_RTCODE_OK;
}

/******************************************************************************
// Name:            ijHuffmanDecode_411_BLS
// Description:     Huffman decoder for 411 image.
// Input Arguments:
//         pStream: Identifies IJBITTREAMSTATE data structure.
//    pDecodeState: Identifies IJDECODESTATE strcuture.
// Output Arguments:
//            pMCUBuf: Identifies decoded MCU.
//                          
// Returns:          
//        [Success] IJX_RTCODE_OK
//        [Failure] IJX_RTCODE_BAGARG
//                  IJX_RTCODE_ERROR
//                     
//                  
******************************************************************************/
IJXRESULT ijHuffmanDecode_411_BLS(Ipp16s **ppMCUBuf, 
                           IJBITTREAMSTATE *pStream,
                           IJDECODESTATE *pDecodeState)
{
    IJXRESULT rtCode;

    rtCode=ippiDecodeHuffman_Direct_JPEG_1u16s (pStream->pBitStream,  
                                         &(pStream->nUsedBufBitsLen),
                                         ppMCUBuf[0],
                                         &(pDecodeState->dcPred[0]),
                                         &(pStream->nMarker),
                                         &(pStream->nPrefetchedBits),
                                         &(pStream->nNumValidPrefetchedBits),
                                         pDecodeState->paIdxDCHuffTable[0],
                                         pDecodeState->paIdxACHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    rtCode=ippiDecodeHuffman_Direct_JPEG_1u16s (pStream->pBitStream,  
                                         &(pStream->nUsedBufBitsLen),
                                         &(ppMCUBuf[0][64]),
                                         &(pDecodeState->dcPred[0]),
                                         &(pStream->nMarker),
                                         &(pStream->nPrefetchedBits),
                                         &(pStream->nNumValidPrefetchedBits),
                                         pDecodeState->paIdxDCHuffTable[0],
                                         pDecodeState->paIdxACHuffTable[0]);

#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    rtCode=ippiDecodeHuffman_Direct_JPEG_1u16s (pStream->pBitStream,  
                                         &(pStream->nUsedBufBitsLen),
                                         &(ppMCUBuf[0][128]),
                                         &(pDecodeState->dcPred[0]),
                                         &(pStream->nMarker),
                                         &(pStream->nPrefetchedBits),
                                         &(pStream->nNumValidPrefetchedBits),
                                         pDecodeState->paIdxDCHuffTable[0],
                                         pDecodeState->paIdxACHuffTable[0]);

#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    rtCode=ippiDecodeHuffman_Direct_JPEG_1u16s (pStream->pBitStream,  
                                         &(pStream->nUsedBufBitsLen),
                                         &(ppMCUBuf[0][192]),
                                         &(pDecodeState->dcPred[0]),
                                         &(pStream->nMarker),
                                         &(pStream->nPrefetchedBits),
                                         &(pStream->nNumValidPrefetchedBits),
                                         pDecodeState->paIdxDCHuffTable[0],
                                         pDecodeState->paIdxACHuffTable[0]);

#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    rtCode=ippiDecodeHuffman_Direct_JPEG_1u16s (pStream->pBitStream,  
                                         &(pStream->nUsedBufBitsLen),
                                         ppMCUBuf[1],
                                         &(pDecodeState->dcPred[1]),
                                         &(pStream->nMarker),
                                         &(pStream->nPrefetchedBits),
                                         &(pStream->nNumValidPrefetchedBits),
                                         pDecodeState->paIdxDCHuffTable[1],
                                         pDecodeState->paIdxACHuffTable[1]);

#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

    rtCode=ippiDecodeHuffman_Direct_JPEG_1u16s (pStream->pBitStream,  
                                         &(pStream->nUsedBufBitsLen),
                                         ppMCUBuf[2],
                                         &(pDecodeState->dcPred[2]),
                                         &(pStream->nMarker),
                                         &(pStream->nPrefetchedBits),
                                         &(pStream->nNumValidPrefetchedBits),
                                         pDecodeState->paIdxDCHuffTable[2],
                                         pDecodeState->paIdxACHuffTable[2]);

//    pDecodeState->counter.nScanMCUCounter++;

    if ( (0<pDecodeState->nRstInterval) ){
        pDecodeState->counter.nDRICounter--;
        if (0==pDecodeState->counter.nDRICounter) {
            ijAdjustRestartIntervalInv(pStream, pDecodeState);
        }
    }

    return IJX_RTCODE_OK;
}

/* EOF */
